use std::env;
use std::fs::File;
use std::io::prelude::*;

fn main() {
    let args: Vec<String> = env::args().collect();

    let config = Config::new(&args);

    println!("what want to search for: {}", config.query);
    println!("in which file: {}", config.filename);

    let mut f = File::open(config.filename).expect("couldn't open file");
    let mut contents = String::new();

    f.read_to_string(&mut contents)
        .expect("couldn't read this file");

    println!("what text: {}", contents);
}

// 定义了 Config 来拥有 args 里面的两个 String 所有权，要用到 clone

// clone 不高效的方式，但简单

struct Config {
    query: String,
    filename: String,
}

impl Config {
    fn new(args: &[String]) -> Result<Config, &'static str> {
        if args.len() < 3 {
            return Err("not enough arguments");
        }
        let query = args[1].clone(); // 这个时候没有 &
        let filename = args[2].clone();
        Ok(Config { query, filename })
    }
}
